var DAO=require('./DAO')
//针对用户数据表操作的模块对象
var chatDAO={
    // 已收藏内容
    collectAll:function(userId,callback){
        DAO('select collect.*,users.userName from users,collect where users.userId=collect.touserId and userId = ?',userId,function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
   
    //typeID==0套餐收藏图片
    lookMenuPic:function(userId,callback){
        DAO('select menuId,menuPic from menu where menuId in(select mm.tomenuId from meal join mm on meal.mealId=mm.tomealId and mealId in(select collect.collectType from users,collect where users.userId=collect.touserId and userId = ? and collect.typeId=0))',[userId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //typeID==1老人收藏图片
    lookPeoplePic:function(userId,callback){
        DAO('select peopleId,healthPic from people where peopleId in(select collect.collectType from users,collect where users.userId=collect.touserId and userId=? and collect.typeId=1)',[userId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    //typeID==2分享收藏图片
    lookSharePic:function(userId,callback){
        DAO('select upmenuId,finishPic from upmenu where upmenuId in(select collect.collectType from users,collect where users.userId=collect.touserId and userId=? and collect.typeId=2)',[userId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },

    //添加套餐到收藏
    collectAdd:function(user,callback){
        DAO('insert into collect(collectTime,collectType,typeId,touserId) VALUES(now(),?,?,?)',[user.collectType,user.typeId,user.userId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    lookCollectAdd:function(userId,callback){
        DAO('select collectId,collectType,typeId,touserId from collect where touserId=?',[userId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },

    //删除已收藏
    collectDelete:function(collectId,callback){
        DAO('DELETE FROM collect where collectId = ?',[collectId],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },

    //已评论内容
    getComment: function (comment, callback) {
        DAO('select * from comment join users on comment.touserId=users.userId where typeId = ? and commentType = ? order by commentTime desc',[comment.typeId,comment.commentType], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },

    //发表评论
    upComment: function (comment, callback) {
        DAO('insert into comment(commentTime,commentContent,commentType,typeId,touserId) values (now(),"?",?,?,?)',[comment.commentContent,comment.commentType,comment.typeId,comment.userId], function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
    delComment: function (commentId, callback) {
        DAO('delete from comment where commentId=?',commentId, function (err, results) {
            if (err) {
                callback(err, null)
            } else {
                callback(null, results)
            }
        })
    },
}
module.exports=chatDAO